const {
  ActivityModel
} = require('../../../utils/activity-model.js')
const activityModel = new ActivityModel()
const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
var utils = require("../../../utils/util.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberId: '',
    assetAvail: 0, //飞豆余额
    shopId: '', //门店id
    authShow: false,
    isDisabled: false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getInfo().then(data => {
      if (data.data.login) {
        this.getUserInfo()
        let param = {
          source: 'page'
        }
        let merchantId = wx.getStorageSync('merchantId')
        app.setTrackerLog(merchantId, 2, 4, 1, param)
      } else {
        this.setData({
          authShow: true
        })
      }
    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    var that = this;
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      wx.setStorageSync('memberId', data.memberId)
      that.setData({
        memberId: data.memberId || "" //会员ID
      }, function () {
        that.getUserAssetDetail()
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 会员资产详情
   */
  getUserAssetDetail() {
    var that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetType = 1; //飞豆
    param.statistics = true; //是否查询统计数据
    param.assetStatus = 0; //0:正常 3:过期
    param.shopId = that.data.shopId; //门店id
    memberModel.userAssetDetail(param).then(data => {
      that.setData({
        assetAvail: utils.formatCurrency(data.assetAvail) || 0, //飞豆余额
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 捕鱼海岛礼品兑换
   */
  exchange(event) {
    let merchantId = wx.getStorageSync('merchantId')
    let c_type = event.currentTarget.dataset.type
    let flybean = event.currentTarget.dataset.flybean

    let m_param = {
      code: c_type
    }
    app.setTrackerLog(merchantId, 2, 4, 2,m_param)
    let that = this;

    wx.showModal({
      title: '提示',
      cancelText: '我再想想',
      confirmText: '我要兑换',
      content: '即将使用' + flybean + '飞豆兑换礼包',
      success (res) {
        if (res.confirm) {
          that.setData({
            isDisabled: true
          })
          let param = {
            codeType: c_type,
            shopId: that.data.shopId,
            merchantId: merchantId
          }
          activityModel.redeemCodeObtain(param).then(data => {
            wx.showToast({
              title: `兑换成功，兑换码已发放，请到消息中心查看`,
              icon: 'none',
              duration: 2000
            })
            that.setData({
              isDisabled: false
            })
            setTimeout(that.getUserAssetDetail, 1500);
          }).catch(err => {
            that.setData({
              isDisabled: false
            })
            wx.showToast({
              title: `${err.error}`,
              icon: 'none',
              duration: 2000
            })
          })
        }
      }
    })
  },

  // 授权成功的回调
  loginSucceed: function () {
    this.getUserInfo()
  },
  goGameUrl(){
    wx.setClipboardData({
      data:'https://lksfish.lkgame.com/sfish-web/promote/17?channel=2400&channelflag=superplayer',
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: `复制成功，请使用浏览器访问下载游戏`,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  }
})